{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# `Nuqleon.Json.Interop.Newtonsoft`\n",
        "\n",
        "Provides interoperability with `Newtonsoft.Json` using `JsonReader` and `JsonWriter` implementations.\n",
        "\n",
        "> **Note:** This assembly was introduced to provide interoperability between the lightweight `Nuqleon.Json.Expressions` object model and the `Newtonsoft.Json` object model, without having to go through intermediate string representations."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Reference the library"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Option 1 - Use a local build\n",
        "\n",
        "If you have built the library locally, run the following cell to load the latest build."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "#r \"bin/Debug/net6.0/Nuqleon.Json.Interop.Newtonsoft.dll\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Option 2 - Use NuGet packages\n",
        "\n",
        "If you want to use the latest published package from NuGet, run the following cell."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "#r \"nuget:Nuqleon.Json.Interop.Newtonsoft,*-*\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## (Optional) Attach a debugger\n",
        "\n",
        "If you'd like to step through the source code of the library while running samples, run the following cell, and follow instructions to start a debugger (e.g. Visual Studio). Navigate to the source code of the library to set breakpoints."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "System.Diagnostics.Debugger.Launch();"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Example\n",
        "\n",
        "An example of converting between Nuqleon's JSON object model and Newtonsoft's JSON serialization support is shown below. Let's first define a custom type."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "record Person\n",
        "{\n",
        "    public string Name { get; init; }\n",
        "    public int Age { get; init; }\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, let's use Newtonsoft JSON to serialize an instance of `Person` to the Nuqleon JSON object model using the `JsonExpressionWriter` interop type."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": "{\"Name\":\"Bart\",\"Age\":21}\r\n"
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "using Newtonsoft.Json;\n",
        "using Nuqleon.Json.Expressions;\n",
        "using Nuqleon.Json.Interop.Newtonsoft;\n",
        "\n",
        "var p = new Person { Name = \"Bart\", Age = 21 };\n",
        "\n",
        "var writer = new JsonExpressionWriter();\n",
        "var serializer = new JsonSerializer();\n",
        "\n",
        "serializer.Serialize(writer, p);\n",
        "\n",
        "Expression expr = writer.Expression;\n",
        "\n",
        "string json = expr.ToString();\n",
        "\n",
        "Console.WriteLine(json);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In the opposite direction, we can use the `JsonExpressionReader` interop type to feed a Nuqleon JSON object to Newtonsoft JSON to perform deserialization."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": "Person { Name = Bart, Age = 21 }\r\n"
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "Expression expr = Expression.Parse(json);\n",
        "\n",
        "var reader = new JsonExpressionReader(expr);\n",
        "var serializer = new JsonSerializer();\n",
        "\n",
        "var res = (Person)serializer.Deserialize(reader, typeof(Person));\n",
        "\n",
        "Console.WriteLine(res);"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".NET (C#)",
      "language": "C#",
      "name": ".net-csharp"
    },
    "language_info": {
      "file_extension": ".cs",
      "mimetype": "text/x-csharp",
      "name": "C#",
      "pygments_lexer": "csharp",
      "version": "9.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}